<?php
require_once 'class/InsertInfo.class.php';
$insertInfo = new InsertInfo();
$chooseContent = new ChooseContent();
$db = DataBase::getInstance();

$countryIdentifiers = $db->getCountriesIdentifiers();
$key = array_search('ALBANIA', $countryIdentifiers);

set_time_limit(160);
$i = 1;
while (@fopen('http://treaties.un.org/doc/Publication/MTDSG/Volume%20I/Chapter%20IV/IV-' . $i . '.en.xml', "r")) {
	$doc = new DOMDocument();
	$doc->load('http://treaties.un.org/doc/Publication/MTDSG/Volume%20I/Chapter%20IV/IV-' . $i . '.en.xml');

	foreach ($doc->getElementsByTagName('Chapter') as $node) {
		$chapterNo = $node->getElementsByTagName('Header')->item(0)->nodeValue;
		$chapterName = $node->getElementsByTagName('Name')->item(0)->nodeValue;
	}
	$chapterID = $insertInfo->insertChapter($chapterNo, $chapterName);
	$title = $doc->getElementsByTagName('Titlesect')->item(0)->nodeValue;
	$conclusion = $doc->getElementsByTagName('Conclusion')->item(0)->nodeValue;
	$conclusionData = $chooseContent->getConclusionData($conclusion);
	foreach ($doc->getElementsByTagName('EIF') as $node) {
		$EF = $node->getElementsByTagName('Labeltext')->item(0)->nodeValue;
	}

	$efData = $chooseContent->getEFData($EF);

	foreach ($doc->getElementsByTagName('Registration') as $node) {
		$registration = $node->getElementsByTagName('Labeltext')->item(0)->nodeValue;
	}

	$registrationData = $chooseContent->getRegistrationData($registration);

	foreach ($doc->getElementsByTagName('TreatyText') as $node) {
		$treatyText = $doc->getElementsByTagName('Text')->item(0)->nodeValue;
	}
	foreach ($doc->getElementsByTagName('TreatyNote') as $node) {
		if (isset($doc->getElementsByTagName('Text')->item(1)->nodeValue)) {
			$note = $doc->getElementsByTagName('Text')->item(1)->nodeValue;
		} else {
			$note = ' ';
		}
		$fullTreatyText = ' ';
	}

	$treatyID = $insertInfo->insertTreaty($chapterID, $title, $conclusionData['date'], $conclusionData['place'], $efData['date'], 
		$efData['text'], $registrationData['date'], $registrationData['No'], $treatyText, $fullTreatyText, $note);

	foreach ($doc->getElementsByTagName('Participants') as $node1) {
		foreach ($node1->getElementsByTagName('Tbody') as $node2) {
			foreach ($node2->getElementsByTagName('Rows') as $node3) {
				foreach ($node3->getElementsByTagName('Row') as $node4) {
					$countryNameString = $node4->getElementsByTagName('Entry')->item(0)->nodeValue;
					$countryName = $chooseContent->getCountryName($countryNameString);
					$signature = $node4->getElementsByTagName('Entry')->item(1)->nodeValue;
					if (empty($signature)) {
						$signature = '0000-00-00';
					} else {
						$signature = $chooseContent->strToDate($signature);
					}
					$ratification = $node4->getElementsByTagName('Entry')->item(2)->nodeValue;
					$ratificationData = $chooseContent->getRatificationData($ratification);
					$countryID = $insertInfo->insertCountry($countryName, $countryIdentifiers);
					$cTreatyID = $insertInfo->insertCTreaty($treatyID, $countryID, $signature, 
						$ratificationData['date'], $ratificationData['details']);

				}
			}
		}
	}
	$i++;
}
echo json_encode('Data successfully updated!');
?>